<?php 
/**
 *  跑过户数据
 */
date_default_timezone_set("Asia/Shanghai");

set_time_limit(0);

if (isset ( $_SERVER ['REQUEST_URI'] )) {
    die ( 'error' );
}
if (! defined ( 'API_ROOT_PATH' )) {
    define ( 'API_ROOT_PATH', dirname ( __FILE__ ) );
}

require_once (API_ROOT_PATH . '/../inc/Config.php');
require_once (API_ROOT_PATH . '/../inc/dblink.php');
require_once (API_ROOT_PATH . '/../inc/Mysql.php');


$db = new Mysql ( MYSQLDB, MYSQLDB_USER, MYSQLDB_PWD, MYSQLDB_DATABASE );



$sql = "select count(*) as num,order_id from au_transfer_log1 group by order_id ";

$transfer_log = $db ->fetchAll($sql);

foreach ($transfer_log as $key => $value) {

	$sid_sql = "select c.sid,c.transfer_limit from au_order as o ,au_cars as c where o.car_id = c.car_id and o.order_id = ".$value['order_id'];

	$sInfo = $db ->fetchOne($sid_sql);

	$sid = $sInfo['sid'];

	$transfer_limit = $sInfo['transfer_limit'];

	if ($value['num'] == 1) {

		$sqlOne = "select * from au_transfer_log1 where order_id =".$value['order_id'];

		$oneInfo = $db ->fetchOne($sqlOne);

		if ($oneInfo['transfer_to'] > 0) {

			$transfer_to = $oneInfo['transfer_to'];

		}else{
			$transfer_to = $transfer_limit;
		}

		$insert_sql = "insert into au_transfer_log (`order_id`,`emp_id`,`transfer_to`,`man_id`,`result`,`sid`,`createtime`,`last_update`) values ('".$oneInfo['order_id']."','".$oneInfo['emp_id']."','".$transfer_to."','".$oneInfo['man_id']."','".$oneInfo['result']."','".$sid."','".$oneInfo['createtime']."','".$oneInfo['last_update']."')";

		$res = $db ->query($insert_sql);

        $transfer_id = $db ->insertId();

		if ($oneInfo['result'] == 1) {

			$sql1 = "update au_transfer_log set startremark = '".$oneInfo['remark']."' where transfer_id = ".$transfer_id;

			$res1 = $db ->query($sql1);
		}
		if ($oneInfo['result'] == 2) {

			$sql2 = "update au_transfer_log set succremark = '".$oneInfo['remark']."' where transfer_id = ".$transfer_id;

			$res2 = $db ->query($sql2);
		}
		
	}elseif ($value['num'] > 1) {
		
		$first_sql = "select * from au_transfer_log1 where order_id = '".$value['order_id']."' order by result desc limit 1";

		$firstInfo = $db ->fetchOne($first_sql);

		if ($firstInfo['transfer_to'] > 0) {

			$transfer_to = $firstInfo['transfer_to'];
			
		}else{
			$transfer_to = $transfer_limit;
		}

		$insert_sql = "insert into au_transfer_log (`order_id`,`emp_id`,`transfer_to`,`man_id`,`result`,`succremark`,`sid`,`last_update`) values ('".$firstInfo['order_id']."','".$firstInfo['emp_id']."','".$transfer_to."','".$firstInfo['man_id']."','".$firstInfo['result']."','".$firstInfo['remark']."','".$sid."','".$firstInfo['last_update']."')";

		$res = $db ->query($insert_sql);

		$transfer_id = $db ->insertId();

		$last_sql = "select * from au_transfer_log1 where order_id = '".$value['order_id']."' order by result asc limit 1";

		$lastInfo = $db ->fetchOne($last_sql);

		$sql3 = "update au_transfer_log set startremark = '".$lastInfo['remark']."', createtime = '".$lastInfo['createtime']."' where transfer_id = ".$transfer_id;

		$res3 = $db ->query($sql3);

	}

}

$db->close();

unset($db);  

 $db = null;


// Insert into au_transfer_log (`order_id`,`result`,`old_transfer_id`,`min_transfer_id`) select order_id,max(result),max(transfer_id),min(transfer_id) from au_transfer_log1 group by order_id;

// 


// Insert into au_transfer_log (`order_id`,`result`,`old_transfer_id`,`min_transfer_id`) select order_id,max(result),max(transfer_id),min(transfer_id) from au_transfer_log1 group by order_id ;


// update au_transfer_log as tl,au_transfer_log1 as tl1 set tl.succremark = tl1.remark,tl.last_update = tl1.last_update where tl.old_transfer_id = tl1.transfer_id and tl.result = 2;

// update au_transfer_log as tl,au_transfer_log1 as tl1 set tl.last_update = tl1.last_update where tl.old_transfer_id = tl1.transfer_id and tl.result = 3;

// update au_transfer_log as tl,au_transfer_log1 as tl1 set tl.startremark = tl1.remark where tl.old_transfer_id = tl1.transfer_id and tl.result = 1;



// update au_transfer_log as tl,au_transfer_log1 as tl1 set tl.createtime = tl1.createtime where tl.old_transfer_id = tl1.transfer_id and tl.result = 0;

// update au_transfer_log as tl,au_transfer_log1 as tl1 set tl.createtime = tl1.createtime,tl.transfer_to = tl1.transfer_to,tl.emp_id = tl1.emp_id,tl.startremark = tl1.remark where tl.old_transfer_id = tl1.transfer_id and tl.result = 1;

// update au_transfer_log as tl,au_transfer_log1 as tl1,au_transfer_log1 as tl2 set tl.createtime = tl2.createtime,tl.transfer_to = tl2.transfer_to,tl.startremark = tl2.remark,tl.emp_id = tl1.emp_id,tl.succremark = tl1.remack,tl.last_update = tl1 = last_update where tl.old_transfer_id = tl1.transfer_id and tl.min_transfer_id = tl2.transfer_id and tl.result = 2;

// update au_transfer_log as tl,au_transfer_log1 as tl1 set tl.createtime = tl1.createtime,tl.transfer_to = tl1.transfer_to,tl.emp_id = tl1.emp_id,tl.startremark = tl1.remark where tl.old_transfer_id = tl1.transfer_id and tl.result = 3;


// update au_transfer_log as tl, au_order as o ,au_cars as c set tl.sid = c.sid where tl.order_id = o.order_id and o.car_id = c.car_id;
 ?>